<?php
require_once(DIR_LIBRARY."orm".DS."orm.php");
require_once(DIR_LIBRARY."orm".DS."acl".DS."permission.php");

class AclAdminUser extends ORM
{
    protected $tablename = "admin_user";
    protected $dsn = AclPermission::DSN;

    public static function getUserMenu($user_id, $tree = true)
    {
        $user_id = (int)$user_id;
        if (!$user_id)
        {
            throw new Exception("no user id");
        }

        $menu = array();
        $adminuser = self::factory();
        $adminuser->appendWhere('user_id="'.$user_id.'"');
        $adminuser->load();
        $role_list = $adminuser->getField("role_list");

        if (!$role_list)
        {
            return $menu;
        }
        unset($adminuser);

        require_once(DIR_LIBRARY."orm".DS."acl".DS."role.php");
        $role = AclRole::factory();
        $role->appendWhere('id in('.implode(",", $role_list).')');
        $rs = $role->execute();

        if (!$rs->isSuccess())
        {
            throw new Exception("db query fail");
        }

        $access_ids = array();
        while ($row = $rs->fetchRow())
        {
            $access_ids = array_merge($access_ids, $row["access_list"]); 
        }

        if (!$access_ids)
        {
            return $menu;
        }
        unset($role);

        require_once(DIR_LIBRARY."orm".DS."acl".DS."access.php");
        $menu = AclAccess::getUserMenuByAccessIds($access_ids, $tree);

        return $menu;
    }

    public function loadByUserId($user_id)
    {
        if (!$user_id)
        {
            throw new Exception("no user id");
        }

        $this->appendWhere('user_id="'.$user_id.'"');
        $this->load();

        return $this;
    }

    public function create()
    {
        if (!$this->getField("user_id"))
        {
            throw new Exception("no user_id passed in");
        }

        if (!$this->getPassword())
        {
            throw new Exception("no password passed in");
        }

        if (!$this->getField("role_list"))
        {
            throw new Exception("no role list");
        }

        $user_id = $this->getField("user_id");
        $adminuser = self::factory();
        $adminuser->appendWhere('user_id="'.$user_id.'"');
        $adminuser->load();

        if ($adminuser->loaded)
        {
            unset($adminuser);
            throw new Exception("exists admin user for user_id ".$user_id);
        }

        $this->setCreated(time());

        return parent::create();
    }

    public function update($user_id)
    {
        if (NULL !== $this->getPassword() && !$this->getPassword())
        {
            throw new Exception("password can not be empty");
        }

        if (NULL !== $this->getField("role_list") && !$this->getField("role_list"))
        {
            throw new Exception("no role list");
        }

        return parent::update();
    }

    public function setPassword($password)
    {
        parent::setPassword(md5($password));
    }


    public function delete($user_id = NULL)
    {
        if (NULL !== $user_id)
        {
            $this->appendWhere("user_id=".$user_id);
        }

        return parent::delete();
    }

    public function setField($name, $value)
    {
        if ("role_list" == $name)
        {
            if (!$value)
            {
                throw new Exception("role ids can not be empty");
            }

            if (!is_array($value))
            {
                $value = array($value);
            }

            $value = AclPermission::buildListFromArray($value);
        }
        parent::setField($name, $value);
    }

    public function getField($name)
    {
        $value = parent::getField($name);
        if (("role_list" == $name) && !is_array($value))
        {
            $value = AclPermission::resolveList($value);
        }

        return $value;
    }
    public static function factory($id = NULL)
    {
        return new self($id);
    }

} //end
